import { format, getDay, isToday, differenceInDays } from "date-fns";

// 格式化时间，传入标准时间格式（new Date）
export function dateFormat(date, str) {
  return format(myDate(date), str);
}

// 时间转星期
export function dateWeek(params) {
  let date = new Date(params);
  if (isToday(date)) {
    return "今天";
  } else {
    let weekday = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
    return weekday[getDay(date)];
  }
}

export function dateDifferenceInDays(in_date, out_date) {
  if (in_date instanceof Date && out_date instanceof Date) {
    return differenceInDays(out_date, in_date);
  } else {
    console.error("时间格式不正确");
    return 0;
  }
}

// 时间转星期
export function getDateWeek(params) {
  let m = new Date(params);
  let week = "";
  if (m.getDay() == 0) week = "星期日";
  if (m.getDay() == 1) week = "星期一";
  if (m.getDay() == 2) week = "星期二";
  if (m.getDay() == 3) week = "星期三";
  if (m.getDay() == 4) week = "星期四";
  if (m.getDay() == 5) week = "星期五";
  if (m.getDay() == 6) week = "星期六";
  return week;
}

// 分钟文字转化
export function ComputeMinutes(params) {
  if (params) {
    if (Number(params) < 60) {
      return params + "m";
    } else {
      let hTotal = Math.floor(params / 60); // 小时
      let mTotal = Math.floor(params % 60); // 小时
      if (mTotal === 0) {
        return hTotal + "h";
      } else {
        return hTotal + "h" + mTotal + "m";
      }
    }
  } else {
    return 0;
  }
}

// 分钟文字转化
export function ComputeMinutesT(params) {
  if (params) {
    if (Number(params) < 60) {
      return params + "分钟";
    } else {
      let hTotal = Math.floor(params / 60); // 小时
      let mTotal = Math.floor(params % 60); // 小时
      if (mTotal === 0) {
        return hTotal + "小时";
      } else {
        return hTotal + "小时" + mTotal + "分钟";
      }
    }
  } else {
    return 0;
  }
}

// 防抖方法
export function debounce(func, wait = 1000) {
  let timeout;
  return function() {
    let context = this;
    let args = arguments;
    if (timeout) clearTimeout(timeout);
    timeout = setTimeout(() => {
      func.apply(context, args);
    }, wait);
  };
}

// 航司logo拼接imgurl
export function getAirlinLogo(airline) {
  return "https://tmc.himyidea.com/download/pic/airlines/" + airline + ".gif";
}

// 折扣转化1
export const textDiscount = num_test => {
  let num = Number(num_test);
  if (num < 10 && num > 0) {
    return num + "折";
  } else if (num === 10) {
    return "全价";
  } else if (num > 10) {
    return num * 10 + "%";
  } else {
    return "";
  }
};

// 折扣转化2 带括号
export const textDiscount2 = num => {
  if (num < 10 && num > 0) {
    return `(${num}折)`;
  } else if (num === 10) {
    return "(全价)";
  } else if (num > 10) {
    return `(${num * 10}%)`;
  } else {
    return "";
  }
};

// 判断当前时间是否再参数区间
export const isDuringDate = (start, end) => {
  const curTime = new Date().getTime();
  const dptTime = myDate(start).getTime();
  const arrTime = myDate(end).getTime();
  if (curTime >= dptTime && curTime <= arrTime) {
    return true;
  } else {
    return false;
  }
};

// 解决js小数点加法精度问题   num1,num2 默认是为0
export const jsAddition = (num1 = 0, num2 = 0) => {
  return (num1 * 100 + num2 * 100) / 100;
};

// 加公共方法往这个注释上面加 （写好方法是干什么）

// 手机号脱敏
function hideInfoPhone(params, type) {
  if (!params) {
    return "";
  }
  if (sessionStorage.is_admin_login) {
    return params;
  }
  if (type === "0") {
    return params;
  }
  let valLen = params.length;
  return params.substring(0, 3) + "****" + params.substring(valLen - 4);
}

// 身份证脱敏
function hideInfoId(params, type) {
  if (!params) {
    return "";
  }
  if (sessionStorage.is_admin_login) {
    return params;
  }
  if (type === "0") {
    return params;
  }
  let valLen = params.length;
  if (valLen < 7) {
    return "*******";
  } else {
    let xingxing = "";
    for (let i = 0; i < valLen - 7; i++) {
      xingxing = xingxing + "*";
    }
    return params.substring(0, 4) + xingxing + params.substring(valLen - 3);
  }
}

// 兼容ios newDate
function myDate(dateString) {
  if (!dateString) {
    return new Date();
  }
  try {
    return new Date(dateString);
  } catch (e) {
    return new Date(dateString.replace(/-/g, "/"));
  }
}

const usePublic = {
  install(app) {
    app.config.globalProperties.$MyDate = myDate;
    app.config.globalProperties.$HideInfoId = hideInfoId;
    app.config.globalProperties.$HideInfoPhone = hideInfoPhone;
  }
};

export { usePublic };
// export {
//   idType,
//   HotelOrderStatus,
//   orderStatus,
//   TrainSeatType,
//   TrainOrderStatusStyle,
//   TrainOrderStatus,
//   ticketStatus,
//   TrainTicketStatus,
//   TrainOrderChannel,
//   Channel,
//   flight_order_status_style,
//   orderChannel,
//   CabinType,
//   FilterTicketStatus,
//   FlightOrderStatus,
//   payTypeFilter
// } from "./enumeration.js";
